package edu.hsbremen.teamscore.controller;

import android.content.Context;

/**
 * Klasse, welche zwei String Arrays hält um zwischen ihnen Werte zu ersetzen. 
 * @author basster
 *
 */
public class StringTransformer {
	
	/**
	 * Das Array mit den alten Strings
	 */
	private String[] _oldStrings;
	private String[] _newStrings;
	
	public StringTransformer(Context c, int oldId, int newId) throws UnequalArrayLengthException {
		_oldStrings = c.getResources().getStringArray(oldId);
		_newStrings = c.getResources().getStringArray(newId);
		
		checkArrays();
	}
	
	public StringTransformer(String[] oldStrings, String[] newStrings) throws UnequalArrayLengthException {
		_oldStrings = oldStrings;
		_newStrings = newStrings;
		
		checkArrays();
	}
	
	public StringTransformer(Context c, int oldId, String[] newStrings) throws UnequalArrayLengthException {
		_oldStrings = c.getResources().getStringArray(oldId);
		_newStrings = newStrings;
		
		checkArrays();
	}
	
	public StringTransformer(Context c, String[] oldStrings, int newId) throws UnequalArrayLengthException {
		_oldStrings = oldStrings;
		_newStrings = c.getResources().getStringArray(newId);
		
		checkArrays();
	}
	
	private void checkArrays() throws UnequalArrayLengthException {
		if (_oldStrings.length != _newStrings.length) {
			throw new UnequalArrayLengthException();
		}
	}
	
	public String getOldString(String string) throws CorrespondingStringNotFoundException {
		for (int i = 0; i < _newStrings.length; i++) {
			if (_newStrings[i].equalsIgnoreCase(string)) {
				return _oldStrings[i];
			}
		}
		
		throw new CorrespondingStringNotFoundException();
	}
	
	public String getNewString(String string) throws CorrespondingStringNotFoundException {
		for (int i = 0; i < _oldStrings.length; i++) {
			if (_oldStrings[i].equalsIgnoreCase(string)) {
				return _newStrings[i];
			}
		}
		
		throw new CorrespondingStringNotFoundException();
	}
	
	public String[] getNewStrings() {
		
		return _newStrings;
	}
	
	public String[] getOldStrings() {
		return _oldStrings;
	}
	
	@SuppressWarnings("serial")
	public class UnequalArrayLengthException extends Exception {
		public UnequalArrayLengthException() {
			super("String Arrays don't have the same size!");
		}
	}
	
	@SuppressWarnings("serial")
	public class CorrespondingStringNotFoundException extends Exception {
		public CorrespondingStringNotFoundException() {
			super("String not found!");
		}
	}
}
